/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hongdeyan.view;


import com.alibaba.fastjson.JSON;
import com.hongdeyan.constant.RequestStatus;
import com.hongdeyan.constant.RespondStatus;
import com.hongdeyan.message_model.Request;
import com.hongdeyan.message_model.Respond;
import com.hongdeyan.model.Duty;
import com.hongdeyan.model.User;
import com.hongdeyan.server.NioClient;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author hdy
 */
@Slf4j
public class RegisterView extends javax.swing.JFrame {

    /**
     * Creates new form Register
     */
    public RegisterView() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕尺寸对象
        Dimension myframe = this.getSize();//获取当前窗体的尺寸对象
        int w = (screen.width / 2 - myframe.width);//水平位置
        int h = (screen.height / 2 - myframe.height);//垂直位置
        setLocation(w, h);
        jLabel1 = new javax.swing.JLabel();
        usernameInput = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        passwordConfiredInput = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        passwordInput = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        consumer = new javax.swing.JRadioButton();
        chef = new javax.swing.JRadioButton();
        takeOut = new javax.swing.JRadioButton();
        registerButton = new javax.swing.JButton();

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String usernameInputText = usernameInput.getText();
                String passwordInputText = passwordInput.getText();
                String passwordConfiredInputText = passwordConfiredInput.getText();
                boolean consumerSelected = consumer.isSelected();
                boolean takeOutSelected = takeOut.isSelected();
                boolean chefSelected = chef.isSelected();
                if ("".equals(usernameInputText) || "".equals(passwordConfiredInputText) ||
                        "".equals(passwordConfiredInputText)) {
                    JOptionPane.showMessageDialog(null, "请输入完整的数据", "输入错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if (!(passwordConfiredInputText.equals(passwordInputText))) {
                    JOptionPane.showMessageDialog(null, "两次密码不一样", "输入错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if (!consumerSelected && !takeOutSelected && !chefSelected) {
                    JOptionPane.showMessageDialog(null, "请选择注册身份", "输入错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                NioClient client = NioClient.getInstance();

                Request request = new Request();
                request.setCode(RequestStatus.REGISTER.getCode());
                User user = new User();
                user.setUsername(usernameInputText);
                user.setPassword(passwordConfiredInputText);
                Duty duty = new Duty(consumer.getText());
                user.setDuty(duty);
                request.setMessage(JSON.toJSONString(user));
                client.send(request, new NioClient.SendBack() {
                    @Override
                    public void get(Respond back) {
                        log.info(back +"");
                        if (back.getCode() == RespondStatus.REGISTER_FAIL.getCode()) {
                            JOptionPane.showMessageDialog(null, back.getMessage(), "注册失败", JOptionPane.ERROR_MESSAGE);
                        } else if(back.getCode() == RespondStatus.REGISTER_SUCCESS.getCode()){
                            JOptionPane.showMessageDialog(null, RespondStatus.REGISTER_SUCCESS.getMessage(), "注册成功", JOptionPane.INFORMATION_MESSAGE);
                            //销毁当前窗体
                            RegisterView.this.dispose();
                        }
                    }
                });
            }
        });


        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("用户名:");

        jLabel2.setText("密码:");

        jLabel3.setText("确认密码:");

        jLabel4.setText("职位:");

        consumer.setText("消费者");

        chef.setText("厨师");

        takeOut.setText("外卖小哥");

        registerButton.setText("立即注册");

        jLabel7.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
        jLabel7.setText("注册账户");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel7)
                                .addGap(150, 150, 150))
                        .addGroup(layout.createSequentialGroup()
                                .addGap(64, 64, 64)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(6, 6, 6)
                                                .addComponent(registerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addComponent(usernameInput, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                .addGroup(layout.createSequentialGroup()
                                                                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addComponent(passwordInput, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                .addGroup(layout.createSequentialGroup()
                                                                        .addComponent(jLabel3)
                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addComponent(passwordConfiredInput, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(consumer)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(chef)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(takeOut)
                                                                .addGap(16, 16, 16)))
                                                .addGap(23, 23, 23))))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(14, 14, 14)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(usernameInput, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(passwordInput, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(passwordConfiredInput, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(consumer)
                                        .addComponent(chef)
                                        .addComponent(takeOut))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(registerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(68, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    // Variables declaration - do not modify
    private javax.swing.JRadioButton chef;
    private javax.swing.JRadioButton consumer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField passwordConfiredInput;
    private javax.swing.JTextField passwordInput;
    private javax.swing.JButton registerButton;
    private javax.swing.JRadioButton takeOut;
    private javax.swing.JTextField usernameInput;
    // End of variables declaration
}
